package com.gh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gh.entity.Lend;
import com.gh.entity.LendItem;
import com.gh.entity.LendItemReturn;
import com.gh.entity.LendReturn;
import com.gh.mapper.*;
import com.gh.service.LendItemReturnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gh.service.UserBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;

/**
 * <p>
 * 标的出借回款记录表 服务实现类
 * </p>
 *
 * @author Eric
 * @since 2021-09-06
 */
@Service
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {

    @Autowired
    private LendMapper lendMapper;
    @Autowired
    private LendReturnMapper lendReturnMapper;
    @Autowired
    private LendItemMapper lendItemMapper;
    @Autowired
    private UserBindService userBindService;
    @Override
    public List<LendItemReturn> selectByLendId(Long lendId, Long userId) {
        LambdaQueryWrapper<LendItemReturn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LendItemReturn::getLendId, lendId)
                .eq(LendItemReturn::getInvestUserId, userId)
                .orderByAsc(LendItemReturn::getCurrentPeriod);
        List<LendItemReturn> lendItemReturnList = baseMapper.selectList(queryWrapper);
        return lendItemReturnList;
    }

    /**
     * 通过还款计划的id找到对应的回款计划数据，组装data参数中需要的list
     * @param lendReturnId
     * @return
     */
    @Override
    public List<Map<String, Object>> addReturnDetail(Long lendReturnId) {
        List<LendItemReturn> lendItemReturnList = this.selectLendItemReturnList(lendReturnId);
        //获取标的
        LendReturn lendReturn = lendReturnMapper.selectById(lendReturnId);
        Lend lend = lendMapper.selectById(lendReturn.getLendId());
        List<Map<String, Object>> lendItemReturnDetailList = new ArrayList<>();
        lendItemReturnList.forEach(lendItemReturn -> {
            Map<String, Object> map = new HashMap<>();
            map.put("agentProjectCode",lend.getLendNo());//项目编号
            Long lendItemId = lendItemReturn.getLendItemId();
            LendItem lendItem = lendItemMapper.selectById(lendItemId);
            map.put("voteBillNo",lendItem.getLendItemNo());//投资编号
            Long investUserId = lendItemReturn.getInvestUserId();
            String bindCode = userBindService.getBindCodeByUserId(investUserId);
            map.put("toBindCode",bindCode);//投资人绑定码
            map.put("transitAmt",lendItemReturn.getTotal());//还款金额
            map.put("baseAmt",lendItemReturn.getInvestAmount());//还款本金
            map.put("benifitAmt",lendItemReturn.getInterest());//还款利息
            map.put("feeAmt",new BigDecimal(0));
            lendItemReturnDetailList.add(map);
        });
        return lendItemReturnDetailList;
    }

    @Override
    public List<LendItemReturn> selectLendItemReturnList(Long lendReturnId) {
        LambdaQueryWrapper<LendItemReturn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LendItemReturn::getLendReturnId,lendReturnId);
        List<LendItemReturn> lendItemReturnList = baseMapper.selectList(queryWrapper);
        return lendItemReturnList;
    }
}
